package com.jic.market.impl;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.orm.Pagination;
import com.jic.common.base.vo.AdminOperate;
import com.jic.market.entity.LargeTurntableTemplateEntity;
import com.jic.market.exception.BussinessExcetionEnum;
import com.jic.market.mapper.LargeTurntableTemplateEntityMapper;
import com.jic.market.request.largeTurntableTemplate.LargeTurntableTemplateRequest;
import com.jic.market.response.LargeTurntableTemplateResponse;
import com.jic.market.response.PageResponse;
import com.jic.market.service.LargeTurntableTemplateService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * description: LargeTurntableTemplateServiceImpl
 * date: 2020/7/7 16:40
 * author: wangys
 * version: 1.0
 */
@Service
public class LargeTurntableTemplateServiceImpl implements LargeTurntableTemplateService {

    @Autowired
    LargeTurntableTemplateEntityMapper largeTurntableTemplateEntityMapper;

    @Override
    public PageResponse<LargeTurntableTemplateResponse> listByPage(LargeTurntableTemplateRequest request) {
        AdminOperate adminOperate = request.getAdminOperate();
        LargeTurntableTemplateEntity largeTurntableTemplateEntity = new LargeTurntableTemplateEntity();
        largeTurntableTemplateEntity.setCreateStartTime(request.getCreateStartTime());
        largeTurntableTemplateEntity.setCreateEndTime(request.getCreateEndTime());
        largeTurntableTemplateEntity.setTemplateType(request.getTemplateType());
        largeTurntableTemplateEntity.setTemplateName(request.getTemplateName());
        largeTurntableTemplateEntity.setDeptId(adminOperate.getDeptId());
        largeTurntableTemplateEntity.setDeleteFlag(0);
        List<LargeTurntableTemplateEntity> ofPage = largeTurntableTemplateEntityMapper.findOfPageExt(largeTurntableTemplateEntity, Pagination.build(request.getStart(), request.getLimit()));
        List<LargeTurntableTemplateResponse> collect = ofPage.stream().map(lar -> {
            LargeTurntableTemplateResponse response = new LargeTurntableTemplateResponse();
            BeanUtils.copyProperties(lar, response);
            response.setDescJson(JSONObject.parseObject(lar.getDescJson()));
            return response;
        }).collect(Collectors.toList());

        int count = largeTurntableTemplateEntityMapper.countOfPageExt(largeTurntableTemplateEntity);
        return new PageResponse().setRows(collect).setTotal(count);
    }


    @Override
    public LargeTurntableTemplateResponse detail(LargeTurntableTemplateRequest request) {
        LargeTurntableTemplateResponse response = new LargeTurntableTemplateResponse();
        LargeTurntableTemplateEntity largeTurntableTemplateEntity = largeTurntableTemplateEntityMapper.queryById(request.getId());
        BeanUtils.copyProperties(largeTurntableTemplateEntity, response);
        response.setDescJson(JSONObject.parseObject(largeTurntableTemplateEntity.getDescJson()));
        return response;
    }

    @Override
    public Long delete(LargeTurntableTemplateRequest request) {
        LargeTurntableTemplateEntity largeTurntableTemplateEntity = new LargeTurntableTemplateEntity();
        largeTurntableTemplateEntity.setDeleteFlag(1);
        largeTurntableTemplateEntity.setId(request.getId());
        largeTurntableTemplateEntityMapper.update(largeTurntableTemplateEntity);
        return request.getId();
    }

    @Override
    public Long createOrUpdate(LargeTurntableTemplateRequest request) {
        AdminOperate adminOperate = request.getAdminOperate();
        LargeTurntableTemplateEntity largeTurntableTemplateEntity = new LargeTurntableTemplateEntity();
        largeTurntableTemplateEntity.setTemplateName(request.getTemplateName());
        largeTurntableTemplateEntity.setTemplateType(1);
        largeTurntableTemplateEntity.setDescJson(JSONObject.toJSONString(request.getDescJson()));
        largeTurntableTemplateEntity.setTemplateId(request.getTemplateId());
        largeTurntableTemplateEntity.setUpdator(adminOperate.getOperator());
        largeTurntableTemplateEntity.setUpdatorName(adminOperate.getOperateName());
        //根据模板名称查询模板
        LargeTurntableTemplateEntity param = new LargeTurntableTemplateEntity();
        param.setTemplateName(request.getTemplateName());
        param.setDeleteFlag(0);
        param.setDeptId(adminOperate.getDeptId());
        List<LargeTurntableTemplateEntity> largeTurntableTemplateEntities = largeTurntableTemplateEntityMapper.queryByTemplateName(param);
        if (request.getId() != null) {
            if(CollectionUtils.isEmpty(largeTurntableTemplateEntities) || largeTurntableTemplateEntities.get(0).getId().equals(request.getId())){
                largeTurntableTemplateEntity.setId(request.getId());
                largeTurntableTemplateEntityMapper.update(largeTurntableTemplateEntity);
            }else {
                throw BussinessExcetionEnum.LARGE_TURNTABLE_TEMPLATE_NAME_ERROR.buildException();
            }
        } else {
            if(CollectionUtils.isNotEmpty(largeTurntableTemplateEntities)){
                throw BussinessExcetionEnum.LARGE_TURNTABLE_TEMPLATE_NAME_ERROR.buildException();
            }
            largeTurntableTemplateEntity.setDeleteFlag(0);
            largeTurntableTemplateEntity.setDeptId(adminOperate.getDeptId());
            largeTurntableTemplateEntity.setCreator(adminOperate.getOperator());
            largeTurntableTemplateEntity.setCreatorName(adminOperate.getOperateName());
            largeTurntableTemplateEntityMapper.insert(largeTurntableTemplateEntity);
        }
        return largeTurntableTemplateEntity.getId();
    }

    @Override
    public List<LargeTurntableTemplateResponse> findAll(LargeTurntableTemplateRequest request) {
        AdminOperate adminOperate = request.getAdminOperate();
        LargeTurntableTemplateEntity largeTurntableTemplateEntity = new LargeTurntableTemplateEntity();
        largeTurntableTemplateEntity.setCreateStartTime(request.getCreateStartTime());
        largeTurntableTemplateEntity.setCreateEndTime(request.getCreateEndTime());
        largeTurntableTemplateEntity.setTemplateType(request.getTemplateType());
        largeTurntableTemplateEntity.setTemplateName(request.getTemplateName());
        largeTurntableTemplateEntity.setDeptId(adminOperate.getDeptId());
        largeTurntableTemplateEntity.setDeleteFlag(0);
        //系统模板类型，不需要deptId
        if(request.getTemplateType() != null && request.getTemplateType() == 2){
            largeTurntableTemplateEntity.setDeptId(null);
        }
        List<LargeTurntableTemplateEntity> all = largeTurntableTemplateEntityMapper.findAllExt(largeTurntableTemplateEntity);
        List<LargeTurntableTemplateResponse> collect = all.stream().map(x -> {
            LargeTurntableTemplateResponse response = new LargeTurntableTemplateResponse();
            BeanUtils.copyProperties(x, response);
            response.setDescJson(JSONObject.parseObject(x.getDescJson()));
            return response;
        }).collect(Collectors.toList());

        return collect;
    }
}
